package com.bluedot.bank.action;

import com.bluedot.bank.dao.Pagination;
import com.bluedot.bank.dao.customer.Customer;
import com.bluedot.bank.dao.customer.CustomerConditons;
import com.bluedot.bank.dao.customer.CustomerDao;
import com.bluedot.bank.dao.customer.CustomerDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class FindCustomerAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Pagination<Customer> pagination = new Pagination<Customer>();
	private int filtername;
	private String c_value;

	/**
	 * @return the filtername
	 */
	public int getFiltername() {
		return filtername;
	}

	/**
	 * @param filtername
	 *            the filtername to set
	 */
	public void setFiltername(int filtername) {
		this.filtername = filtername;
	}

	@Override
	public String execute() throws Exception {
		CustomerDao customerDao = new CustomerDaoImpl();
		switch (filtername) {
		case 1:
			pagesResult(pagination, null);
			customerDao.browse(pagination);
			break;
		case 2:
			pagesResult(pagination, getCondition("cIdFilter", "customerId",
					Long.parseLong(c_value)));
			customerDao.browse(pagination);
			break;
		case 3:
			pagesResult(pagination, getCondition("lastNameFilter",
					"lastNameKey", "%" + c_value + "%"));
			customerDao.browse(pagination);
			break;
		case 4:
			pagesResult(pagination, getCondition("firstNameFilter",
					"firstNameKey", "%" + c_value + "%"));
			customerDao.browse(pagination);
			break;

		default:
			pagesResult(pagination, null);
			customerDao.browse(pagination);
			break;
		}
		return SUCCESS;
	}

	/**
	 * @return the pagination
	 */
	public Pagination<Customer> getPagination() {
		return pagination;
	}

	/**
	 * @param pagination
	 *            the pagination to set
	 */
	public void setPagination(Pagination<Customer> pagination) {
		this.pagination = pagination;
	}

	void pagesResult(Pagination<Customer> pagination,
			CustomerConditons conditons) {
		if (this.pagination == null) {
			this.pagination = new Pagination<Customer>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}

		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		pagination.setConditon(conditons);
	}

	CustomerConditons getCondition(String filterName, String key, Object o) {
		CustomerConditons conditons = new CustomerConditons();
		conditons.setFilterName(filterName);
		conditons.setKey(key);
		conditons.setO(o);
		return conditons;
	}

	/**
	 * @return the c_value
	 */
	public String getC_value() {
		return c_value;
	}

	/**
	 * @param c_value
	 *            the c_value to set
	 */
	public void setC_value(String c_value) {
		this.c_value = c_value;
	}

}
